﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;

[assembly: CLSCompliant(true)]
namespace OA.UserManager
{
    /// <summary>
    /// 系统具体功能
    /// </summary>
    public class Feature
    {
        #region 属性
        public int Id { get; set; }
        public string Name { get; set; }
        public int ActionId { get; set; }
        public string ActionName { get; set; }
        #endregion
        #region 方法
        /// <summary>
        /// 添加角色对应的功能权限
        /// </summary>
        /// <param name="feature">功能权限</param>
        /// <param name="role">角色</param>
        /// <param name="createBy">操作员</param>
        /// <returns></returns>
        public static bool AddFeatureToRole(Feature feature, Role role, int createBy)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO [E_OA].[dbo].[Purview] ([RoleId] ,[FeatureId] ,[CreateOn] ,[CreateBy])");
            strSql.Append("VALUES (@RoleId,@FeatureId,@CreateOn,@CreateBy )");
            SqlParameter[] parameters = { 
                                            new SqlParameter("@RolId",role.Id),
                                            new SqlParameter("@FeatureId",feature.Id),
                                            new SqlParameter("@CreateOn",DateTime.Now),
                                            new SqlParameter("@CreateBy",createBy)
                                        };
            int rows = DbHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 移除角色的功能权限
        /// </summary>
        /// <param name="feature">功能</param>
        /// <param name="role">角色</param>
        /// <returns></returns>
        public static bool RemoveFeatureFromRole(Feature feature, Role role)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM [E_OA].[dbo].[Purview]");
            strSql.Append("where FeatureId=@featureId and RoleId=@roldId");
            SqlParameter[] parameters = { 
                                            new SqlParameter("@featureId",feature.Id),
                                            new SqlParameter("@roleId",role.Id)
                                        };
            int rows = DbHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
        /// <summary>
        /// 分页获取角色所有的功能权限
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <param name="page">当前页码</param>
        /// <param name="size">每页显示记录数</param>
        /// <returns></returns>
        public static QueryResult<Feature> GetFeaturesByRole(int roleId, int page, int size)
        {
            QueryResult<Feature> result = new QueryResult<Feature>(page, size);
            #region 设置存储过程参数
            SqlParameter[] parameters = {
               new SqlParameter("@tblName",SqlDbType.NVarChar,200),
               new SqlParameter("@fldName",SqlDbType.NVarChar,500),
               new SqlParameter("@pageSize",SqlDbType.Int,4),
               new SqlParameter("@page",SqlDbType.Int,4),
               new SqlParameter("@pageCount",SqlDbType.Int,4),
               new SqlParameter("@Counts",SqlDbType.Int,4),
               new SqlParameter("@fldSort",SqlDbType.NVarChar,200),
               new SqlParameter("@Sort",SqlDbType.Bit,1),
               new SqlParameter("@strCondition",SqlDbType.NVarChar,1000),
               new SqlParameter("@ID",SqlDbType.NVarChar,150),
               new SqlParameter("@Dist",SqlDbType.Bit,1)
            };
            parameters[0].Value = "dbo.Actions INNER JOIN  dbo.FeatureModules ON dbo.Actions.Id = dbo.FeatureModules.Id";
            parameters[0].Value += " INNER JOIN dbo.Features ON dbo.Actions.Id = dbo.Features.ActionId";
            parameters[0].Value += "AND dbo.FeatureModules.Id = dbo.Features.FeatureModuleId INNER JOIN dbo.Purview ON dbo.Features.Id = dbo.Purview.FeatureId";
            parameters[1].Value = "dbo.Features.Id,  dbo.FeatureModules.Name,dbo.Features.ActionId, dbo.Actions.Name";
            parameters[2].Value = size;
            parameters[3].Value = page;
            parameters[4].Direction = ParameterDirection.Output;
            parameters[5].Direction = ParameterDirection.Output;
            parameters[6].Value = "CreateOn";
            parameters[7].Value = true;
            parameters[8].Value = "Purview.RoleId=" + roleId;
            parameters[9].Value = "Roles.Id";
            parameters[10].Value = false;
            #endregion
            SqlDataReader reader = DbHelper.RunProcedure("proc_QueryPage", parameters);
            Feature feature = new Feature();
            while (reader.Read())
            {
                feature.Id = reader.GetInt32(0);
                feature.Name = reader.GetString(1);
                feature.ActionId = reader.GetInt32(2);
                feature.ActionName = reader.GetString(3);
                result.DataList.Add(feature);
            }
            result.Count = int.Parse(parameters[5].Value.ToString());
            result.PageCount = int.Parse(parameters[4].Value.ToString());
            return result;
        }

        public static QueryResult<Feature> GetAllFeatures(int page, int size)
        {
            QueryResult<Feature> result = new QueryResult<Feature>(page, size);
            #region 设置存储过程参数
            SqlParameter[] parameters = {
               new SqlParameter("@tblName",SqlDbType.NVarChar,200),
               new SqlParameter("@fldName",SqlDbType.NVarChar,500),
               new SqlParameter("@pageSize",SqlDbType.Int,4),
               new SqlParameter("@page",SqlDbType.Int,4),
               new SqlParameter("@pageCount",SqlDbType.Int,4),
               new SqlParameter("@Counts",SqlDbType.Int,4),
               new SqlParameter("@fldSort",SqlDbType.NVarChar,200),
               new SqlParameter("@Sort",SqlDbType.Bit,1),
               new SqlParameter("@strCondition",SqlDbType.NVarChar,1000),
               new SqlParameter("@ID",SqlDbType.NVarChar,150),
               new SqlParameter("@Dist",SqlDbType.Bit,1)
            };
            parameters[0].Value = "View_Features";
            parameters[1].Value = "Id,FeatureModuleName,ActionId,ActionName";
            parameters[2].Value = size;
            parameters[3].Value = page;
            parameters[4].Direction = ParameterDirection.Output;
            parameters[5].Direction = ParameterDirection.Output;
            parameters[6].Value = "CreateOn";
            parameters[7].Value = true;
            parameters[8].Value = "";
            parameters[9].Value = "Id";
            parameters[10].Value = false;
            #endregion
            SqlDataReader reader = DbHelper.RunProcedure("proc_QueryPage", parameters);
            Feature feature = new Feature();
            while (reader.Read())
            {
                feature.Id = reader.GetInt32(0);
                feature.Name = reader.GetString(1);
                feature.ActionId = reader.GetInt32(2);
                feature.ActionName = reader.GetString(3);
                result.DataList.Add(feature);
            }
            result.Count = int.Parse(parameters[5].Value.ToString());
            result.PageCount = int.Parse(parameters[4].Value.ToString());
            return result;
        }
        /// <summary>
        /// 获取具有某功能权限的角色列表
        /// </summary>
        /// <param name="featureId"></param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static QueryResult<Role> GetRolesByFeature(int featureId, int page, int size)
        {
            QueryResult<Role> result = new QueryResult<Role>(page, size);
            #region 设置存储过程参数
            SqlParameter[] parameters = {
               new SqlParameter("@tblName",SqlDbType.NVarChar,200),
               new SqlParameter("@fldName",SqlDbType.NVarChar,500),
               new SqlParameter("@pageSize",SqlDbType.Int,4),
               new SqlParameter("@page",SqlDbType.Int,4),
               new SqlParameter("@pageCount",SqlDbType.Int,4),
               new SqlParameter("@Counts",SqlDbType.Int,4),
               new SqlParameter("@fldSort",SqlDbType.NVarChar,200),
               new SqlParameter("@Sort",SqlDbType.Bit,1),
               new SqlParameter("@strCondition",SqlDbType.NVarChar,1000),
               new SqlParameter("@ID",SqlDbType.NVarChar,150),
               new SqlParameter("@Dist",SqlDbType.Bit,1)
            };
            parameters[0].Value = "Roles INNER JOIN Purview On Purview.RoleId=Roles.Id";
            parameters[1].Value = "Roles.Id,Roles.Name,Roles.Description,Roles.CreateOn,Roles.CreateBy,Roles.ModifiedOn,Roles.ModifiedBy";
            parameters[2].Value = size;
            parameters[3].Value = page;
            parameters[4].Direction = ParameterDirection.Output;
            parameters[5].Direction = ParameterDirection.Output;
            parameters[6].Value = "CreateOn";
            parameters[7].Value = true;
            parameters[8].Value = "Purview.FeatureId=" + featureId;
            parameters[9].Value = "Roles.Id";
            parameters[10].Value = false;
            #endregion
            SqlDataReader reader = DbHelper.RunProcedure("proc_QueryPage", parameters);
            Role role = new Role();
            while (reader.Read())
            {
                role.Id = reader.GetInt16(1);
                role.Name = reader.GetString(2);
                role.Description = reader.GetString(3);
                role.CreateOn = reader.GetDateTime(4);
                role.CreateBy = reader.GetInt32(5);
                role.ModifiedOn = reader.GetDateTime(6);
                role.ModifiedBy = reader.GetInt32(7);
                result.DataList.Add(role);
            }
            result.Count = int.Parse(parameters[5].Value.ToString());
            result.PageCount = int.Parse(parameters[4].Value.ToString());
            return result;
        }


        #endregion
    }
}
